package com.yonyou.gmmc.service.points.service;

import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.marketing.common.FrameHttpUtil;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.CheckUtil;
import org.marketing.common.util.CommonUtils;
import org.marketing.common.util.DateUtil;
import org.marketing.common.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.points.constant.PointsConstant;
import com.yonyou.gmmc.service.points.dao.TtWxOrderPayMapper;
import com.yonyou.gmmc.service.points.model.TtWxOrderPay;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class WxPayService {
	private static Logger logger = LoggerFactory.getLogger(WxPayService.class);
	@Autowired
	private TtWxOrderPayMapper ttWxOrderPayMapper;

	@Value("${IDEALERSS_URL}")
	private String idealerurl;
	@Value("${SECRET_KEY}")
	private String secretKey;
	@Value("${CHANNEL}")
	private String channel;
	@Value("${IDEALERCODE}")
	private String idealerCode;
	@Value("${GROUPCODE}")
	private String groupCode;
	@Value("${SELLERMCHID}")
	private String sellerMchId;

	/**
	 * 微信公众号支付接口
	 * 
	 * @param paMap
	 * @return
	 */
	public Map<String, Object> orderReqJson(Map<String, String> paMap) {
		logger.info("WxPayService.orderReqJson==>start...");
		Map<String, Object> result = new HashMap<>();
		TtWxOrderPay wop1 = new TtWxOrderPay();
		wop1.setSellerMchId(sellerMchId);
		wop1.setBuyerOpenId(paMap.get("buyerOpenId"));
		wop1.setAmount(new BigDecimal(paMap.get("amount")));
		wop1.setMotormasterName(paMap.get("motormasterName"));
		wop1.setPlateNumber(paMap.get("plateNumber"));
		wop1.setGoodsName(paMap.get("goodsName"));
		wop1.setSubject(paMap.get("subject"));
		wop1.setTradeMemo(paMap.get("tradeMemo"));
		wop1.setTradeStatus(PointsConstant.WX_PAY_STATUS1);
		wop1.setOrderId(paMap.get("orderId"));
		wop1.setOperator(Integer.valueOf(paMap.get("operator")));
		wop1.setCreateDate(new Date());
		wop1.setCreateBy(PointsConstant.OPT_USER);
		ttWxOrderPayMapper.insertSelective(wop1);
		try {
			// 向idealer下单
			Map<String, Object> getCarMap = new HashMap<>();
			// 发生时间
			getCarMap.put("occurtime", new Date().getTime());
			// 经销商代码类型 10081001(销售) 10081002(售后)
			getCarMap.put("dealerType", "10081002");
			// 经销商代码
			getCarMap.put("dealerCode", idealerCode);
			// 消息来源
			getCarMap.put("msgSrc", "10091001");
			// 品牌代码
			getCarMap.put("groupCode", groupCode);
			// 来源单据类型
			getCarMap.put("billType", "10101001");
			// 来源单据编号
			getCarMap.put("billCode", "10101001");
			String paySeq = channel+"_"+groupCode+"_"+idealerCode+"10101001"+"10101001";
			// 付款流水号
			getCarMap.put("paySeq", paySeq);
			// 下单人
			getCarMap.put("operator", paMap.get("operator"));
			// 收款方
			getCarMap.put("sellerMchId", sellerMchId);
			// 付款方openId
			getCarMap.put("buyerOpenId", paMap.get("buyerOpenId"));
			// 交易性质
			getCarMap.put("tradeNature", "10751001");
			// 金额
			getCarMap.put("amount", paMap.get("amount"));
			// 车主姓名
			getCarMap.put("motormasterName", paMap.get("motormasterName"));
			// 车牌号
			getCarMap.put("plateNumber", paMap.get("plateNumber"));
			// 商品名称
			getCarMap.put("goodsName", paMap.get("goodsName"));
			// 订单标题
			getCarMap.put("subject", paMap.get("subject"));
			// 商品描述
			getCarMap.put("tradeMemo", paMap.get("tradeMemo"));
			getCarMap.put("channel", channel);
			logger.info("idealerRequest xiadan map = " + getCarMap);
			String jsonResponse = FrameHttpUtil.post1(idealerurl+"publicPay/orderReqJson", CommonUtils.generateSign(getCarMap, secretKey));
			logger.info("jsonResponse="+jsonResponse);
			
			if (!CheckUtil.NullOrEmpty(jsonResponse)) {
				JSONObject returnObject = JSONObject.fromObject(jsonResponse);
				if (!CheckUtil.NullOrEmpty(returnObject) && "9120".equals(returnObject.getString("code"))) {

					JSONObject resultMsg = JSONObject.fromObject(returnObject.get("data"));
					if (!CheckUtil.NullOrEmpty(resultMsg) && "S".equals(resultMsg.getString("gateReturnType"))) {
						Example ex = new Example(TtWxOrderPay.class);
						ex.createCriteria().andEqualTo("orderId", paMap.get("orderId"));
						TtWxOrderPay wop = new TtWxOrderPay();
						if (!CheckUtil.NullOrEmpty(resultMsg.get("tradeId"))) {
							wop.setTradeId(resultMsg.getString("tradeId"));
						}
						if (!CheckUtil.NullOrEmpty(resultMsg.get("jsapiAppId"))) {
							wop.setJsapiAppId(resultMsg.getString("jsapiAppId"));
						}
						if (!CheckUtil.NullOrEmpty(resultMsg.get("jsapiNonceStr"))) {
							wop.setJsapiNonceStr(resultMsg.getString("jsapiNonceStr"));					
						}
						if (!CheckUtil.NullOrEmpty(resultMsg.get("jsapiPackage"))) {
							wop.setJsapiPackage(resultMsg.getString("jsapiPackage"));
						}
						if (!CheckUtil.NullOrEmpty(resultMsg.get("jsapiPaySign"))) {
							wop.setJsapiPaySign(resultMsg.getString("jsapiPaySign"));
						}
						if (!CheckUtil.NullOrEmpty(resultMsg.get("jsapiSignType"))) {
							wop.setJsapiSignType(resultMsg.getString("jsapiSignType"));
						}
						if (!CheckUtil.NullOrEmpty(resultMsg.get("jsapiTimeStamp"))) {
							wop.setJsapiTimeStamp(DateUtil.dateFormat(DateUtil.timeStamp2Date(resultMsg.get("jsapiTimeStamp").toString(),"yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss"));
						}
						wop.setPaySeq(paySeq);
						wop.setTradeStatus(PointsConstant.WX_PAY_STATUS4);
						wop.setUpdateDate(new Date());
						wop.setUpdateBy(PointsConstant.OPT_USER);
						ttWxOrderPayMapper.updateByExampleSelective(wop, ex);
					}else{
						Example ex = new Example(TtWxOrderPay.class);
						ex.createCriteria().andEqualTo("orderId", paMap.get("orderId"));
						TtWxOrderPay wop = new TtWxOrderPay();
						wop.setTradeStatus(PointsConstant.WX_PAY_STATUS5);
						wop.setUpdateDate(new Date());
						wop.setUpdateBy(PointsConstant.OPT_USER);
						ttWxOrderPayMapper.updateByExampleSelective(wop, ex);
						result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
						result.put(ResultMsgConstants.ERR_MSG, resultMsg.get("gateReturnMessage"));
						return result;
					}
					
				} else {
					result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
					result.put(ResultMsgConstants.ERR_MSG, returnObject.get("message"));
					return result;
				}
			} else {
				result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
				result.put(ResultMsgConstants.ERR_MSG, "支付平台异常");
				return result;
			}

		} catch (Exception e) {
			logger.error("orderReqJson ====> " + e);
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "系统繁忙,请稍后重试。");
			return result;
		}
		return result;
	}
	/**
	 * 退款接口
	 * 
	 * @param paMap
	 * @return
	 */
	public Map<String, Object> cancelTrans(Map<String, String> paMap) {
		logger.info("WxPayService.cancelTrans==>start...");
		Map<String, Object> result = new HashMap<>();
		TtWxOrderPay wop = new TtWxOrderPay();
		wop.setOrderId(paMap.get("orderId"));
		TtWxOrderPay wxOrderPay = ttWxOrderPayMapper.selectOne(wop);
		if (!CheckUtil.NullOrEmpty(wxOrderPay) && wxOrderPay.getTradeStatus()!=PointsConstant.WX_PAY_STATUS4) {
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "订单未支付或正在退款中");
			return result;
		}
		//校验支付平台的订单状态
		List<String> pList = new ArrayList<>();
		pList.add(wxOrderPay.getTradeId());
		List<Map<String, Object>> resultList = queryTradeResult(pList);
		if (!CheckUtil.NullOrEmpty(resultList)) {
			Integer ts = Integer.valueOf(resultList.get(0).get("tradeStatus").toString());
			if(PointsConstant.WX_PAY_STATUS4!=ts){
				result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
				result.put(ResultMsgConstants.ERR_MSG, "订单未支付或正在退款中");
				return result;
			}
		}
		try {
			// 向idealer下单
			Map<String, Object> getCarMap = new HashMap<>();
			// 发生时间
			getCarMap.put("occurtime", new Date().getTime());
			// 经销商代码类型 10081001(销售) 10081002(售后)
			getCarMap.put("dealerType", "10081002");
			// 经销商代码
			getCarMap.put("dealerCode", idealerCode);
			// 服务平台订单id
			getCarMap.put("tradeId", wxOrderPay.getTradeId());
			// 品牌代码
			getCarMap.put("groupCode", groupCode);
			// 退款金额
			getCarMap.put("orderAmount", wxOrderPay.getAmount());
			// 退款说明
			getCarMap.put("orderNote", paMap.get("orderNote"));
			// 退款人
			getCarMap.put("refundPerson", paMap.get("refundPerson"));
			// 备注信息
			getCarMap.put("remark", paMap.get("remark"));
			getCarMap.put("channel", channel);
			logger.info("idealerRequest xiadan map = " + getCarMap);
			String jsonResponse = FrameHttpUtil.post1(idealerurl+"pay/cancelTrans", CommonUtils.generateSign(getCarMap, secretKey));
			logger.info("jsonResponse="+jsonResponse);
			
			if (!CheckUtil.NullOrEmpty(jsonResponse)) {
				JSONObject returnObject = JSONObject.fromObject(jsonResponse);
				if (!CheckUtil.NullOrEmpty(returnObject) && "9120".equals(returnObject.getString("code"))) {

					JSONObject resultMsg = JSONObject.fromObject(returnObject.get("data"));
					String s  = resultMsg.get("gateReturnType")==null?"":resultMsg.getString("gateReturnType");
					if (!CheckUtil.NullOrEmpty(s) && "S".equals(s)) {
						//更新订单状态
						Example ex = new Example(TtWxOrderPay.class);
						ex.createCriteria().andEqualTo("orderId", paMap.get("orderId"));
						TtWxOrderPay wop1 = new TtWxOrderPay();
						wop1.setOrderNote(paMap.get("orderNote"));
						wop1.setRefundPerson(paMap.get("refundPerson"));
						wop1.setRemark(paMap.get("remark"));
						wop1.setTradeStatus(PointsConstant.WX_PAY_STATUS7);
						wop1.setUpdateDate(new Date());
						wop1.setUpdateBy(PointsConstant.OPT_USER);
						ttWxOrderPayMapper.updateByExampleSelective(wop1, ex);
					}else if (!CheckUtil.NullOrEmpty(s) && "D".equals(s)) {
						Example ex = new Example(TtWxOrderPay.class);
						ex.createCriteria().andEqualTo("orderId", paMap.get("orderId"));
						TtWxOrderPay wop1 = new TtWxOrderPay();
						wop1.setOrderNote(paMap.get("orderNote"));
						wop1.setRefundPerson(paMap.get("refundPerson"));
						wop1.setRemark(paMap.get("remark"));
						wop1.setTradeStatus(PointsConstant.WX_PAY_STATUS3);
						wop1.setUpdateDate(new Date());
						wop1.setUpdateBy(PointsConstant.OPT_USER);
						ttWxOrderPayMapper.updateByExampleSelective(wop1, ex);
					}else{
						result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
						result.put(ResultMsgConstants.ERR_MSG, resultMsg.get("gateReturnMessage"));
						return result;
					}
				} else {
					result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
					result.put(ResultMsgConstants.ERR_MSG, returnObject.get("message"));
					return result;
				}
			} else {
				result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
				result.put(ResultMsgConstants.ERR_MSG, "支付平台异常");
				return result;
			}

		} catch (Exception e) {
			logger.error("cancelTrans ====> " + e);
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "系统繁忙,请稍后重试。");
			return result;
		}
		return result;
	}
	/**
	 * 查询(付款/退款)接口
	 * 
	 * @param list
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryTradeResult(List<String> list) {
		logger.info("WxPayService.queryTradeResult==>start...");
		List<Map<String, Object>> resultList = new ArrayList<>();
		Map<String, Object> result = new HashMap<>();
		if (!CheckUtil.NullOrEmpty(list)) {
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "参数不能为空");
			resultList.add(result);
			return resultList;
		}
		List<Map<String, Object>> paList = new ArrayList<>();
		for (String str : list) {
			Map<String, Object> m = new HashMap<>();
			m.put("tradeId", str);
			paList.add(m);
		}
		String tradeIds = JsonUtils.parseListForMapsToJsonArrayStr(paList);
		try {
			// 向idealer下单
			Map<String, Object> getCarMap = new HashMap<>();
			// 发生时间
			getCarMap.put("occurtime", new Date().getTime());
			// 服务平台订单id
			getCarMap.put("tradeIds", tradeIds);
			// 品牌代码
			getCarMap.put("groupCode", groupCode);
			getCarMap.put("channel", channel);
			logger.info("idealerRequest xiadan map = " + getCarMap);
			String jsonResponse = FrameHttpUtil.post1(idealerurl+"pay/queryTradeResult", CommonUtils.generateSign(getCarMap, secretKey));
			logger.info("jsonResponse="+jsonResponse);
			
			if (!CheckUtil.NullOrEmpty(jsonResponse)) {
				JSONObject returnObject = JSONObject.fromObject(jsonResponse);
				if (!CheckUtil.NullOrEmpty(returnObject) && "9120".equals(returnObject.getString("code"))) {

					JSONArray resultMsg = JSONArray.fromObject(returnObject.get("data"));
					if (!CheckUtil.NullOrEmpty(resultMsg)) {
						for (Object object : resultMsg) {
							Map<String, Object> reMap = (Map<String, Object>) object;
							resultList.add(reMap);
							String gateReturnType = reMap.get("gateReturnType").toString();
							String tradeId = reMap.get("tradeId").toString();
							if("S".equals(gateReturnType)){
								String tradeStatus = reMap.get("tradeStatus").toString();
								//更新订单状态
								Example ex = new Example(TtWxOrderPay.class);
								ex.createCriteria().andEqualTo("tradeId", tradeId);
								TtWxOrderPay wop1 = new TtWxOrderPay();
								wop1.setTradeStatus(Integer.valueOf(tradeStatus));
								wop1.setUpdateDate(new Date());
								wop1.setUpdateBy(PointsConstant.OPT_USER);
								ttWxOrderPayMapper.updateByExampleSelective(wop1, ex);
							}
						}
					}else{
						result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
						result.put(ResultMsgConstants.ERR_MSG, "支付平台异常");
						resultList.add(result);
						return resultList;
					}
				} else {
					result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
					result.put(ResultMsgConstants.ERR_MSG, returnObject.get("message"));
					resultList.add(result);
					return resultList;
				}
			} else {
				result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
				result.put(ResultMsgConstants.ERR_MSG, "支付平台异常");
				resultList.add(result);
				return resultList;
			}

		} catch (Exception e) {
			logger.error("queryTradeResult ====> " + e);
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "系统繁忙,请稍后重试。");
			resultList.add(result);
			return resultList;
		}
		return resultList;
	}
	/**
	 * 取消支付接口（若订单支付成功，则不能进行取消支付）
	 * 
	 * @param list
	 * @return
	 */
	public Map<String, Object> cancelOrder(String tradeId) {
		logger.info("WxPayService.cancelOrder==>start...");
		Map<String, Object> result = new HashMap<>();
		if (!CheckUtil.NullOrEmpty(tradeId)) {
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "参数不能为空");
			return result;
		}
		List<String> paList = new ArrayList<>();
		paList.add(tradeId);
		List<Map<String, Object>> reLsit = queryTradeResult(paList);
		if (!CheckUtil.NullOrEmpty(reLsit)) {
			Integer ts = Integer.valueOf(reLsit.get(0).get("tradeStatus").toString());
			if(PointsConstant.WX_PAY_STATUS4==ts){
				result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
				result.put(ResultMsgConstants.ERR_MSG, "订单支付成功，不能取消");
				return result;
			}
		}
		try {
			// 向idealer下单
			Map<String, Object> getCarMap = new HashMap<>();
			// 发生时间
			getCarMap.put("occurtime", new Date().getTime());
			// 服务平台订单id
			getCarMap.put("tradeId", tradeId);
			getCarMap.put("channel", channel);
			logger.info("idealerRequest xiadan map = " + getCarMap);
			String jsonResponse = FrameHttpUtil.post1(idealerurl+"pay/cancelOrder", CommonUtils.generateSign(getCarMap, secretKey));
			logger.info("jsonResponse="+jsonResponse);
			
			if (!CheckUtil.NullOrEmpty(jsonResponse)) {
				JSONObject returnObject = JSONObject.fromObject(jsonResponse);
				if (!CheckUtil.NullOrEmpty(returnObject) && "9120".equals(returnObject.getString("code"))) {

					JSONObject resultMsg = JSONObject.fromObject(returnObject.get("data"));
					String gateReturnType = resultMsg.getString("gateReturnType");
					if("S".equals(gateReturnType)){
						//更新订单状态
						Example ex = new Example(TtWxOrderPay.class);
						ex.createCriteria().andEqualTo("tradeId", tradeId);
						TtWxOrderPay wop1 = new TtWxOrderPay();
						wop1.setTradeStatus(PointsConstant.WX_PAY_STATUS8);
						wop1.setUpdateDate(new Date());
						wop1.setUpdateBy(PointsConstant.OPT_USER);
						ttWxOrderPayMapper.updateByExampleSelective(wop1, ex);
						result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_SUCCESS);
						result.put(ResultMsgConstants.ERR_MSG, resultMsg.getString("gateReturnMessage"));
					}else{
						result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
						result.put(ResultMsgConstants.ERR_MSG, resultMsg.getString("gateReturnMessage"));
						return result;
					}
				} else {
					result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
					result.put(ResultMsgConstants.ERR_MSG, returnObject.get("message"));
					return result;
				}
			} else {
				result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
				result.put(ResultMsgConstants.ERR_MSG, "支付平台异常");
				return result;
			}

		} catch (Exception e) {
			logger.error("cancelOrder ====> " + e);
			result.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			result.put(ResultMsgConstants.ERR_MSG, "系统繁忙,请稍后重试。");
			return result;
		}
		return result;
	}
	 /**
	 * 支付状态主动通知接口
	 * @param etcoId
	 */
    public Map<String, Object> payStatus(Map<String, Object> param) {
    	Map<String, Object>  result = new HashMap<>();
    	Map<String, Object> ch = checkNull(param);
    	if("0".equals(ch.get("code").toString())){
    		return ch;
    	}
    	result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_SUCCESS);
    	String id = param.get("tradeId").toString();
    	Integer orderStatus = Integer.valueOf(param.get("tradeStatus").toString());
    	TtWxOrderPay wop = new TtWxOrderPay();
    	wop.setTradeId(id);
    	TtWxOrderPay wxOrderPay = ttWxOrderPayMapper.selectOne(wop);
    	if(CheckUtil.NullOrEmpty(wxOrderPay)){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "失败，该订单不存在");
    		return result;
    	}
    	Example ex = new Example(TtWxOrderPay.class);
		ex.createCriteria().andEqualTo("tradeId", id);
		TtWxOrderPay wop1 = new TtWxOrderPay();
		wop1.setTradeStatus(orderStatus);
		wop1.setUpdateDate(new Date());
		wop1.setUpdateBy(PointsConstant.OPT_USER);
		
    	int i = ttWxOrderPayMapper.updateByExampleSelective(wop1, ex);
        if (i==1) {
        	result.put(ResultMsgConstants.RESULT_MESSAGE, "成功");
    		return result;
        } else {
        	result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.RESULT_MSG_CODE_ERROR);
        	result.put(ResultMsgConstants.RESULT_MESSAGE, "调用成功，更新失败");
            return result;
        }
    }
    /**
	 *  校验参数是否为空
	 */
    public Map<String, Object> checkNull(Map<String, Object> param) {
    	Map<String, Object>  result = new HashMap<>();
    	result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_SUCCESS);
    	if(CheckUtil.NullOrEmpty(param.get("sign"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "签名不能为空");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("tradeStatus"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "支付预约单状态不能为空");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("groupCode"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "品牌代码不能为空");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("tradeId"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "交易单号不能为空");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("msgSrc"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "消息来源");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("dealerType"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "经销商代码类型");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("dealerCode"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "经销商代码");
    		return result;
    	}
    	if(CheckUtil.NullOrEmpty(param.get("postTime"))){
    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
    		result.put(ResultMsgConstants.RESULT_MESSAGE, "推送时间");
    		return result;
    	}
    	
//    	if(CheckUtil.NullOrEmpty(param.get("channel"))){
//    		result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.RESULT_MSG_CODE_ERROR);
//    		result.put(ResultMsgConstants.RESULT_MESSAGE, "渠道号不能为空");
//    		return result;
//    	}
    	try {
    		String sign = param.get("sign")==null ? "":param.get("sign").toString();
    		logger.info("idealer---sign===" + sign);
    		param.remove("sign");
//    		param.put("channel", "100");
			Map<String, Object> map = CommonUtils.generateSign(param, secretKey);
			String si = map.get("sign").toString();
			logger.info("localhost---sign===" + si);
			if(!si.equals(sign)){
				result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
				result.put(ResultMsgConstants.RESULT_MESSAGE, "加密不正确");
			}
		} catch (NoSuchAlgorithmException e) {
			logger.info("checkNull===" + e);
			result.put(ResultMsgConstants.RESULT_MSG_CODE, ResultMsgConstants.PAY_MSG_CODE_ERROR);
			result.put(ResultMsgConstants.RESULT_MESSAGE, e.getMessage());
		}
    	return result;
    }
}
